/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package ui.gui.dialogs.settings;

import java.awt.BorderLayout;
import org.xml.sax.*;
import java.awt.Toolkit;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.tree.TreePath;
import javax.xml.parsers.SAXParser;

/**
 *
 * @author Leonhart
 */
public class Settings extends javax.swing.JDialog {
    private static final long serialVersionUID = 1L;

// XMLSETTINGS WINDOWS
    private GlobalSettings _globalSettings = null;
    private FontSettings _fontSettings = null;
    
    /**
     * Creates new form Settings
     */
    public Settings(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocation((int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2-(getWidth()/2), (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/2-(getHeight()/2));
        try{
        SaxParser.GetInstance().parseDocument("c:\\Parameters.xml");     
        }
        catch(Exception exception)
        {
            System.out.println(exception.toString());
        }
        _globalSettings = new GlobalSettings();
        _fontSettings = new FontSettings();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        _ButtonOK = new javax.swing.JButton();
        _ButtonCancel = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Settings");
        setMinimumSize(new java.awt.Dimension(0, 460));
        setPreferredSize(new java.awt.Dimension(940, 460));
        setResizable(false);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setMinimumSize(new java.awt.Dimension(200, 23));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(200, 322));

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("Options");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Environment");
        javax.swing.tree.DefaultMutableTreeNode treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Windows Settings");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Fonts and Colors");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        jTree1.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        jTree1.setVisibleRowCount(10);
        jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTree1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jTree1);

        jPanel2.add(jScrollPane1, java.awt.BorderLayout.WEST);

        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel4.setMinimumSize(new java.awt.Dimension(0, 50));
        jPanel4.setPreferredSize(new java.awt.Dimension(740, 50));
        jPanel4.setLayout(new java.awt.BorderLayout());

        jPanel5.setMinimumSize(new java.awt.Dimension(100, 300));
        jPanel5.setPreferredSize(new java.awt.Dimension(200, 300));
        jPanel5.setLayout(null);

        _ButtonOK.setText("OK");
        _ButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _ButtonOKActionPerformed(evt);
            }
        });
        jPanel5.add(_ButtonOK);
        _ButtonOK.setBounds(110, 10, 70, 30);

        _ButtonCancel.setText("Cancel");
        _ButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                _ButtonCancelActionPerformed(evt);
            }
        });
        jPanel5.add(_ButtonCancel);
        _ButtonCancel.setBounds(10, 10, 80, 30);

        jPanel4.add(jPanel5, java.awt.BorderLayout.EAST);

        jPanel3.add(jPanel4, java.awt.BorderLayout.PAGE_END);

        jPanel1.setLayout(new java.awt.BorderLayout());
        jPanel3.add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel3, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
        TreePath tree = jTree1.getSelectionPath();        
        if(tree.getLastPathComponent().toString() == "Windows Settings")
        {            
            jPanel1.removeAll();
            jPanel1.add(_globalSettings, BorderLayout.CENTER);
            _globalSettings.setVisible(true);
            jPanel1.revalidate();  
            jPanel1.repaint();
            jScrollPane1.repaint();     
        }
        if(tree.getLastPathComponent().toString() == "Fonts and Colors")
        {            
            jPanel1.removeAll();
            jPanel1.add(_fontSettings, BorderLayout.CENTER);
            _fontSettings.setVisible(true);
            jPanel1.revalidate();  
            jPanel1.repaint();
            jScrollPane1.repaint();     
        }
    }//GEN-LAST:event_jTree1ValueChanged

    private void _ButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__ButtonCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event__ButtonCancelActionPerformed

    private void _ButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event__ButtonOKActionPerformed
         try
         {
        ArrayList<XMLSettings> AllSettingsL = new ArrayList<XMLSettings>();         
         AllSettingsL.addAll(_fontSettings.getParameters());
         AllSettingsL.addAll(_globalSettings.getParameters());
         SaxParser.GetInstance().SaveParameters(AllSettingsL);  
         System.out.println("New settings are saved");
         this.dispose();
         }
         catch (Exception ex) {
        	 
         }
    }//GEN-LAST:event__ButtonOKActionPerformed

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton _ButtonCancel;
    private javax.swing.JButton _ButtonOK;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables
}
